package com.msconfig.mvp.view.fragment;

/*
 * author: caojunhao
 * date  : 2020/5/13
 * desc  :
 */

import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.geo.msconfig.R;
import com.msconfig.bean.dialogBean.ProjectionParameterBean;
import com.msconfig.device.GeoLocationData;
import com.msconfig.event.ClickProjectionParameterDeterminerEvent;
import com.msconfig.event.ProjectionDialogDismissEvent;
import com.msconfig.mvp.view.activity.SetupProjectionSelectDialogActivity;
import com.msconfig.mvp.view.activity.base.BaseMvpFragment;
import com.msconfig.mvp.view.activity.base.BasePresenter;
import com.msconfig.utils.DoubleToLocationUtil;
import com.msconfig.utils.GeneralUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;
import java.util.Objects;

public class SetupProjectionParameterFragment extends BaseMvpFragment implements View.OnClickListener {
    public static final String RLPROJECTIONMODE = "rlProjectionMode";
    private static SetupProjectionParameterFragment fragment;
    private GeoLocationData geoLocationData;
    private ImageView ivAutoCentralMeridian;
    private ImageView ivCommonBack;
    private TextView tvCommonTitle;
    private RelativeLayout rlProjectionMode;
    private TextView tvProjectionMode;
    private EditText etCentralMeridian;
    private EditText etNorgaConstant;
    private EditText etEastgaConstant;
    private EditText etProjectionScale;
    private EditText etProjectionHeight;
    private EditText etBaseLatitude;
    private EditText etBaseLogitude;
    private EditText etParallel1;
    private EditText etParallel2;
    private Button btnDetermine;
    private ConstraintLayout clLayout;
    private ConstraintLayout clProjectionHeight;
    private ProjectionParameterBean projectionBean;
    private String mProjectionMode;
    private static final String PROJECTIONMODE="projectionmode";
    private static final String CENTRALMERIDIAN="centralmeridian";
    private static final String NORGACONSTANT="norgaconstant";
    private static final String EASTGACONSTANT="eastgaconstant";
    private static final String PROJECTIONSCALE="projectionscale";
    private static final String PROJECTIONHEIGHT="projectionheight";
    private static final String BASELATITUDE="baselatitude";
    private static final String BASELOGITUDE="baselogitude";
    private static final String ETPARALLEL1="etparallel1";
    private static final String ETPARALLEL2="etparallel2";
    private String model;
    private String baselogitude;
    private String baselatitude;
    private String projectionheight;
    private String projectionscale;
    private String eastgaconstant;
    private String norgaconstant;
    private String centralmeridian;
    private double autoCentralMeridian;


    public static SetupProjectionParameterFragment newInstance(ProjectionParameterBean projectionBean) {
        Bundle args = new Bundle();
        fragment = new SetupProjectionParameterFragment();

        if(projectionBean==null){
            fragment.setArguments(null);
        }else{
            args.putString(PROJECTIONMODE,projectionBean.getProjectionMode());
            args.putString(CENTRALMERIDIAN,projectionBean.getCentralMeridian());
            args.putString(NORGACONSTANT,projectionBean.getNorgaConstant());
            args.putString(EASTGACONSTANT,projectionBean.getEastgaConstant());
            args.putString(PROJECTIONSCALE,projectionBean.getProjectionScale());
            args.putString(PROJECTIONHEIGHT,projectionBean.getProjectionHeight());
            args.putString(BASELATITUDE,projectionBean.getBaseLatitude());
            args.putString(BASELOGITUDE,projectionBean.getBaseLogitude());
            args.putString(ETPARALLEL1,projectionBean.getEtParallel1());
            args.putString(ETPARALLEL2,projectionBean.getEtParallel2());
            fragment.setArguments(args);
        }
        return fragment;
    }


    @Override
    public void onClick(View v) {
      if(v==ivCommonBack){
          backTo();
      }else if(v==rlProjectionMode){
          Intent intent=new Intent(getActivity(),SetupProjectionSelectDialogActivity.class);
          intent.putExtra(RLPROJECTIONMODE,tvProjectionMode.getText().toString().trim());
          startActivity(intent);
      }else if(v==btnDetermine){
          onClickDetermine();
      }else if(v==ivAutoCentralMeridian){
          if(geoLocationData.getLatitude() !=0 && geoLocationData.getLongitude()!=0 && autoCentralMeridian!=0){
              etCentralMeridian.setText(String.valueOf(autoCentralMeridian));
          }else{
              Toast.makeText(context,"数据采集失败！",Toast.LENGTH_SHORT).show();
          }
      }

    }

    @Override
    protected void createPresenter(List<BasePresenter> mPresenters) {

    }

    //选择对应的投影方式后
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showSelectProjectionParameter(ProjectionDialogDismissEvent event) {
        String projectionMode=event.getStrName();
        tvProjectionMode.setText(projectionMode);
        clLayout.setVisibility(View.VISIBLE);
        clProjectionHeight.setVisibility(View.GONE);
        if(projectionMode.equals(getString(R.string.utm_projection))){//UTM投影，影藏对应部分
            clLayout.setVisibility(View.GONE);
        }else if(projectionMode.equals(getString(R.string.gauss_projection))){
            clProjectionHeight.setVisibility(View.VISIBLE);
        }
        //目的就是为了选择不同的数据时显示的是每个的默认值，只有选择的是读取到的模式时才显示读取的数据
        if(!model.equals(projectionMode)){//选择的模式不是读取到的模式时，显示默认值
            etCentralMeridian.setText("126");
            etNorgaConstant.setText("0");
            etEastgaConstant.setText("500000");
            etProjectionScale.setText("1");
            etProjectionHeight.setText("0");
            etBaseLatitude.setText("0");
            etBaseLogitude.setText("0");
        }else{
            tvProjectionMode.setText(model);
            etCentralMeridian.setText(centralmeridian);
            etNorgaConstant.setText(norgaconstant);
            etEastgaConstant.setText(eastgaconstant);
            etProjectionScale.setText(projectionscale);
            etProjectionHeight.setText(projectionheight);
            etBaseLatitude.setText(baselatitude);
            etBaseLogitude.setText(baselogitude);
        }
        //根据选择的模式计算中央经线
        autoCentralMeridian= GeneralUtil.autoCalculationCenMeridian(projectionMode,geoLocationData.getLongitude());
    }


    @Override
    protected int getLayoutId() {
        return R.layout.layout_fragment_setup_projection_parameter;
    }

    @Override
    protected void initView(View view) {
        ivAutoCentralMeridian=view.findViewById(R.id.iv_auto_central_meridian);
        ivCommonBack=view.findViewById(R.id.iv_common_back);
        tvCommonTitle=view.findViewById(R.id.tv_common_title);
        rlProjectionMode = (RelativeLayout)view.findViewById( R.id.rl_projection_mode );
        tvProjectionMode = (TextView)view.findViewById( R.id.tv_projection_mode );
        etCentralMeridian = (EditText)view.findViewById( R.id.et_central_meridian );
        etNorgaConstant = (EditText)view.findViewById( R.id.et_norga_constant );
        etEastgaConstant = (EditText)view.findViewById( R.id.et_eastga_constant );
        etProjectionScale = (EditText)view.findViewById( R.id.et_projection_scale );
        etProjectionHeight = (EditText)view.findViewById( R.id.et_projection_height );
        etBaseLatitude = (EditText)view.findViewById( R.id.et_base_latitude );
        etBaseLogitude = (EditText)view.findViewById( R.id.et_base_longitude );
        etParallel1 = (EditText)view.findViewById( R.id.et_parallel1 );
        etParallel2 = (EditText)view.findViewById( R.id.et_parallel2 );
        btnDetermine=(Button)view.findViewById(R.id.btn_determine);
        clLayout=(ConstraintLayout)view.findViewById(R.id.cl_layout);
        clProjectionHeight=(ConstraintLayout)view.findViewById(R.id.cl_projection_height);
    }

    @Override
    protected void initListener(View view) {
        btnDetermine.setOnClickListener(this);
        ivCommonBack.setOnClickListener(this);
        rlProjectionMode.setOnClickListener(this);
        ivAutoCentralMeridian.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        tvCommonTitle.setText(R.string.title_projection_parameter);
//        etCentralMeridian.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {
//            @Override
//            public void onFocusChange(View v, boolean hasFocus) {
//                if(hasFocus) {
//                    // 此处为得到焦点时的处理内容
//                    etCentralMeridian.setText(String.valueOf(DoubleToLocationUtil.convertToDecimal(etCentralMeridian.getText().toString().trim())));
//                } else {
//                    // 此处为失去焦点时的处理内容
//                    etCentralMeridian.setText(DoubleToLocationUtil.dblToLocation(Double.parseDouble(etCentralMeridian.getText().toString().trim())));
//                }
//
//            }
//
//        });


        geoLocationData = GeoLocationData.getInstance();
        Bundle bundle = fragment.getArguments();
        if(bundle!=null){
            model=bundle.getString(PROJECTIONMODE);
            centralmeridian=bundle.getString(CENTRALMERIDIAN);
            norgaconstant= bundle.getString(NORGACONSTANT);
            eastgaconstant=bundle.getString(EASTGACONSTANT);
            projectionscale=bundle.getString(PROJECTIONSCALE);
            projectionheight = bundle.getString(PROJECTIONHEIGHT);
            baselatitude=bundle.getString(BASELATITUDE);
            baselogitude=bundle.getString(BASELOGITUDE);
            tvProjectionMode.setText(model);
            etCentralMeridian.setText(centralmeridian);
            etNorgaConstant.setText(norgaconstant);
            etEastgaConstant.setText(eastgaconstant);
            etProjectionScale.setText(projectionscale);
            etProjectionHeight.setText(projectionheight);
            etBaseLatitude.setText(baselatitude);
            etBaseLogitude.setText(baselogitude);
            etParallel1.setText(bundle.getString(ETPARALLEL1));
            etParallel2.setText(bundle.getString(ETPARALLEL2));

            //从外面读取数据进来显示的时候根据model对应布局
            clLayout.setVisibility(View.VISIBLE);
            clProjectionHeight.setVisibility(View.GONE);
            if(Objects.requireNonNull(bundle.getString(PROJECTIONMODE)).equals(getString(R.string.utm_projection))){//UTM投影，影藏对应部分
                clLayout.setVisibility(View.GONE);
            }else if(Objects.requireNonNull(bundle.getString(PROJECTIONMODE)).equals(getString(R.string.gauss_projection))){
                clProjectionHeight.setVisibility(View.VISIBLE);
            }
            autoCentralMeridian= GeneralUtil.autoCalculationCenMeridian(model,geoLocationData.getLongitude());
        }
    }



    private void onClickDetermine() {
        //回调把信息返回回去
        if(tvProjectionMode.getText().toString().trim().equals("") ){
            Toast.makeText(getActivity(), R.string.toast_choose_projection, Toast.LENGTH_SHORT).show();
        }else{
            backTo();
            projectionBean=new ProjectionParameterBean();
            projectionBean.setProjectionMode(tvProjectionMode.getText().toString().trim());
            projectionBean.setCentralMeridian(etCentralMeridian.getText().toString().trim());
            projectionBean.setNorgaConstant(etNorgaConstant.getText().toString().trim());
            projectionBean.setEastgaConstant(etEastgaConstant.getText().toString().trim());
            projectionBean.setProjectionScale(etProjectionScale.getText().toString().trim());
            projectionBean.setProjectionHeight(etProjectionHeight.getText().toString().trim());
            projectionBean.setBaseLatitude(etBaseLatitude.getText().toString().trim());
            projectionBean.setBaseLogitude(etBaseLogitude.getText().toString().trim());
            projectionBean.setEtParallel1(etParallel1.getText().toString().trim());
            projectionBean.setEtParallel2(etParallel2.getText().toString().trim());
            EventBus.getDefault().post(new ClickProjectionParameterDeterminerEvent(projectionBean) );
        }
    }
}
